package com.scada.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scada.hessian.client.impl.MemberBuyServiceVisitorClient;
import com.scada.hessian.client.impl.MemberInfoVisitorClient;
import com.scada.model.bean.MemberInfo;
import com.scada.model.bean.ServiceInfo;
import com.scada.model.bean.User;
import com.scada.model.bean.vo.MemberBuyServiceVO;
import com.scada.model.bean.vo.MemberInfoVO;
import com.scada.model.bean.vo.ReturnTimeEdit;
import com.scada.model.dataTable.ChooseMemberList;
import com.scada.model.dataTable.MemberInfoList;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.model.result.RestResponseBean;
import com.scada.model.search.MemberInfoSearch;

@Controller
@RequestMapping("member")
public class MemberController extends BaseController{

	@Autowired
	private MemberInfoVisitorClient memberInfoVisitorClient;
	@Autowired
	private MemberBuyServiceVisitorClient memberBuyServiceVisitorClient;
	
	@RequestMapping(value = "toMain")
	public String toMain(){
		return "member/list";
	}
	
	@RequestMapping(value = "getMemberInfoPageData")
	@ResponseBody
	public String getMemberInfoPageData(@ModelAttribute MemberInfoSearch memberInfoSearch) {
		
		PageInfo<MemberInfoList> pageInfo = new PageInfo<MemberInfoList>();
        pageInfo.setStart(memberInfoSearch.getStart());
        pageInfo.setPageSize(memberInfoSearch.getLength());
        pageInfo.setDraw(memberInfoSearch.getDraw());
        ResponseBean rb = memberInfoVisitorClient.getMemberInfoPageData(memberInfoSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<MemberInfoList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<MemberInfoList>());
        }
        
		return toJson(pageInfo);
	}
	
	
	@RequestMapping(value = "toSaveMemberForm")
	public String toSaveMemberForm(){
		return "member/addMemberForm";
	}
	
	@RequestMapping(value = "insertMemberInfo")
	@ResponseBody
	public String insertMemberInfo(@ModelAttribute MemberInfo memberInfo, String intentionServiceStr, String custTypeStr) {
		
		ResponseBean rb = new ResponseBean();
		Boolean re = true;
		rb = memberInfoVisitorClient.validateMemberRepeat(memberInfo.getMobile(), memberInfo.getEmail1(), null);
		if(StringUtils.equals("2", rb.getType())){
			Integer count = (Integer)rb.getObject("result");
			if(count>=1){
				re = false;
				rb.failed("当前会员已存在，无法继续购买！");
			}
		}else{
			re = false;
			rb.failed("验证会员是否存在发生错误，无法继续购买！");
		}
		
		if(re) {
			try{
				User user = getCurrentUser();
				memberInfo.setCreateId(user.getId());
				memberInfo.setLastUpdateId(user.getId());
				memberInfoVisitorClient.insertMemberInfo(memberInfo, intentionServiceStr, custTypeStr);
				rb.success();
			}catch(Exception e){
				rb.failed();
			}
		}
		return toJson(rb);
	}
	
	@RequestMapping(value = "toEditMemberForm")
	public ModelAndView toEditMemberForm(Long id, ModelMap model){
		
		MemberInfoVO mi = null;
		try {
			ResponseBean rb = memberInfoVisitorClient.getMemberInfoById(id);
			if(StringUtils.equals("2", rb.getType())){
				mi = (MemberInfoVO)rb.getObject("result");
			}
		}catch (Exception e) {
			
		}
		
		model.put("mi", mi);
		return new ModelAndView("member/editMemberForm");
	}
	
	@RequestMapping(value = "toMemberDetailForm")
	public ModelAndView toMemberDetailForm(Long id, ModelMap model){
		
		MemberInfoVO mi = null;
		try {
			ResponseBean rb = memberInfoVisitorClient.getMemberInfoDetail(id);
			if(StringUtils.equals("2", rb.getType())){
				mi = (MemberInfoVO)rb.getObject("result");
			}
		}catch (Exception e) {
			
		}
		
		model.put("mi", mi);
		return new ModelAndView("member/detail");
	}
	
	
	@RequestMapping(value = "updateMemberInfo")
	@ResponseBody
	public String updateMemberInfo(@ModelAttribute MemberInfo memberInfo, String intentionServiceStr, String custTypeStr) {
		
		ResponseBean rb = new ResponseBean();
		Boolean re = true;
		rb = memberInfoVisitorClient.validateMemberRepeat(memberInfo.getMobile(), memberInfo.getEmail1(), memberInfo.getId());
		if(StringUtils.equals("2", rb.getType())){
			Integer count = (Integer)rb.getObject("result");
			if(count>=1){
				re = false;
				rb.failed("当前会员已存在，无法继续购买！");
			}
		}else{
			re = false;
			rb.failed("验证会员是否存在发生错误，无法继续购买！");
		}
		
		if(re) {
			try{
				User user = getCurrentUser();
				memberInfo.setLastUpdateId(user.getId());
				memberInfoVisitorClient.updateMemberInfo(memberInfo, intentionServiceStr, custTypeStr);
				rb.success();
			}catch(Exception e){
				rb.failed();
			}
		}
		
		return toJson(rb);
	}
	
	@RequestMapping(value = "deleteMemberInfoById")
	@ResponseBody
	public String deleteMembeInfoById(String ids){
		
		ResponseBean rb = new ResponseBean();
		try{
			memberInfoVisitorClient.deleteMemberInfoById(MemberInfo.IsShowCode.NO.getKey(), ids);
			rb.success();
		}catch(Exception e){
			rb.failed();
		}
		return toJson(rb);
	}
	
	@RequestMapping("getSelectMember")
	@ResponseBody
	public String getSelectMember(String pageSize,String currentPage,String searchName) {
		
		RestResponseBean rest=RestResponseBean.getRestResponseBean();
		ResponseBean responseBean=null;
		try{
			responseBean=memberInfoVisitorClient.getSelectMemberData(Integer.parseInt(pageSize), Integer.parseInt(currentPage), searchName);
			if(StringUtils.equals("2", responseBean.getType())){
				rest.setCount((Integer)responseBean.getObject("resultCount"));
				rest.setData((List<ChooseMemberList>)responseBean.getObject("resultList"));
	        }else{
	        	rest.setCount(0);
	        	rest.setData(new ArrayList<ChooseMemberList>());
	        }
		}catch(Exception e){
			rest.setCount(0);
        	rest.setData(new ArrayList<ChooseMemberList>());
		}
		rest.success();
		
		return toJson(rest);
	}
	
	@RequestMapping(value = "toSeeMemberServiceBuy")
	public String toSeeMemberServiceBuy(Long id, ModelMap model){
		model.put("memberId", id);
		return "member/buyService/member/list";
	}
	
	@RequestMapping(value = "toSeeProjcetServiceBuy")
	public String toSeeProjcetServiceBuy(Long id, ModelMap model){
		model.put("memberId", id);
		return "member/buyService/project/list";
	}
	
	@RequestMapping(value = "updateReturnTime")
	@ResponseBody
	public String updateReturnTime(@ModelAttribute ReturnTimeEdit returnTimeEdit) {
		
		ResponseBean rb = memberInfoVisitorClient.updateReturnTime(returnTimeEdit);
		return toJson(rb);
	}
	
	@RequestMapping(value = "toAddMemberService")
	public String toAddMemberService(Long memberId, ModelMap model){
		model.put("memberId", memberId);
		return "member/buyService/member/add";
	}
	
	@RequestMapping(value = "toAddProjectService")
	public String toAddProjectService(Long memberId, ModelMap model){
		model.put("memberId", memberId);
		return "member/buyService/project/add";
	}
	
	@RequestMapping(value = "toAddPlanBuyMember")
	public ModelAndView toAddPlanBuyMember(Long memberId, ModelMap model){
		model.put("memberId", memberId);
		return new ModelAndView("member/planBuy/member/add");
	}
	
	@RequestMapping(value = "toAddPlanBuyProject")
	public ModelAndView toAddPlanBuyProject(Long memberId, ModelMap model){
		model.put("memberId", memberId);
		return new ModelAndView("member/planBuy/project/add");
	}
	
	@RequestMapping(value = "toDetailMemberBuyService")
	public ModelAndView toDetailMemberBuyService(Long id, ModelMap model){
		
		MemberBuyServiceVO mbs = null;
		try {
			ResponseBean rb = memberBuyServiceVisitorClient.getMemberBuyServiceById(id);
			if(StringUtils.equals("2", rb.getType())){
				mbs = (MemberBuyServiceVO)rb.getObject("result");
			}
		}catch (Exception e) {
			
		}
		
		model.put("mbs", mbs);
		if(mbs != null) {
			if(StringUtils.equals(ServiceInfo.ServiceTypeCode.MEMBER.getKey(), mbs.getServiceType())) {
				return new ModelAndView("member/buyService/member/detail");
			}else if(StringUtils.equals(ServiceInfo.ServiceTypeCode.PROJECT.getKey(), mbs.getServiceType())) {
				return new ModelAndView("member/buyService/project/detail");
			}
		}
		
		return null;
		
	}
	
	@RequestMapping(value = "toEditMemberBuyService")
	public ModelAndView toEditMemberBuyService(Long id, ModelMap model){
		
		MemberBuyServiceVO mbs = null;
		try {
			ResponseBean rb = memberBuyServiceVisitorClient.getMemberBuyServiceById(id);
			if(StringUtils.equals("2", rb.getType())){
				mbs = (MemberBuyServiceVO)rb.getObject("result");
			}
		}catch (Exception e) {
			
		}
		
		model.put("mbs", mbs);
		if(mbs != null) {
			if(StringUtils.equals(ServiceInfo.ServiceTypeCode.MEMBER.getKey(), mbs.getServiceType())) {
				return new ModelAndView("member/buyService/member/edit");
			}else if(StringUtils.equals(ServiceInfo.ServiceTypeCode.PROJECT.getKey(), mbs.getServiceType())) {
				return new ModelAndView("member/buyService/project/edit");
			}
		}
		
		return null;
		
	}
}
